package com.apidocs.code.codeutil;

import com.apidocs.code.util.FileUtil;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;

import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class CodeTool {
  private static String TEMPLATE_DIR = "/templates";
  private static String CLASS_TEMPLATE_DIR;
  private static VelocityEngine JAR_ENGINE;

  static {
    try {
      CLASS_TEMPLATE_DIR = CodeTool.class.getResource(TEMPLATE_DIR).getPath();
    } catch (Exception e) {
      CLASS_TEMPLATE_DIR = TEMPLATE_DIR;
    }
    //设置velocity资源加载方式为jar
    Properties jarP = new Properties();
    jarP.setProperty("resource.loader", "jar");
    //设置velocity资源加载方式为file时的处理类
    jarP.setProperty("jar.resource.loader.class", "org.apache.velocity.runtime.resource.loader.JarResourceLoader");
    jarP.setProperty(
      "jar.resource.loader.path",
      "jar:file:" + CodeTool.class.getProtectionDomain().getCodeSource().getLocation().getPath()
    );
    JAR_ENGINE = new VelocityEngine(jarP);
  }

  private VelocityEngine classEngine;
  private String encoding = "UTF-8";
  private String tmplFile;// 锟斤拷锟斤拷
  private String absolutePath;// 锟斤拷锟斤拷
  private Map context = new HashMap();


  public void put(String key, Object value) {
    context.put(key, value);
  }

  public void put(Map paramMap) {
    context.putAll(paramMap);
  }

  public Object get(String key) {
    return context.get(key);
  }


  public String getTmplFile() {
    return tmplFile;
  }

  public void setTmplFile(String tmplFile) {
    this.tmplFile = tmplFile;
  }

  public String getEncoding() {
    return encoding;
  }

  public void setEncoding(String encoding) {
    this.encoding = encoding;
  }

  public String getAbsolutePath() {
    return absolutePath;
  }

  public void setAbsolutePath(String absolutePath) {
    this.absolutePath = absolutePath;
  }

  public Map getContext() {
    return context;
  }

  public void setContext(Map context) {
    this.context = context;
  }

  public void createFileByTmpl() throws Exception {
    Template template;
    try {
      template = classEngine.getTemplate(tmplFile, encoding);
    } catch (Exception e) {
      template = JAR_ENGINE.getTemplate(TEMPLATE_DIR + tmplFile, encoding);
    }
    VelocityContext tmplContext = new VelocityContext(context);
    FileUtil.createFile(absolutePath);
    PrintWriter writer = new PrintWriter(new FileOutputStream(absolutePath), true);
    template.merge(tmplContext, writer);
    writer.flush();
    writer.close();
  }

  public String getText(String text) throws Exception {
    VelocityContext tmplContext = new VelocityContext(context);
    StringWriter w = new StringWriter();
    Velocity.evaluate(tmplContext, w, super.getClass().getName(), text);
    return w.toString();
  }

  public void initVelocity() {
    Properties classP = new Properties();
    classP.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, CLASS_TEMPLATE_DIR);
    classP.setProperty("file.resource.loader.class","org.apache.velocity.runtime.resource.loader.FileResourceLoader");
    classEngine = new VelocityEngine(classP);
  }

  public CodeTool() {
    super();
  }

  public CodeTool(String tmplFile, String absolutePath) {
    super();
    this.tmplFile = tmplFile;
    this.absolutePath = absolutePath;
  }
}
